//
//  WelcomeViewController.swift
//  Weibo10
//
//  Created by mac on 16/8/15.
//  Copyright © 2016年 itheima. All rights reserved.
//

import UIKit
import SDWebImage


// MARK: - 主分类
class WelcomeViewController: UIViewController {
    // MARK: -属性
    /**
    属性必须写在主分支中，否则影响程序的结构
    */
    
    // 背景图像
    private lazy var backImageView: UIImageView = UIImageView(imageName: "ad_background")
    
    // 头像
    private lazy var iconView: UIImageView = {
        let iv = UIImageView(imageName: "avatar_default_big")
        
        // 设置圆角
        iv.layer.cornerRadius = 45
        iv.layer.masksToBounds = true
        
        return iv
    }()
    
    // MARK: -方法
    // 欢迎标签
    private lazy var welcomeLabel: UILabel = UILabel(title: "欢迎归来", fontSize: 18)
    
    // 加载界面
    override func loadView() {
        
        view = backImageView
        
        setupUI()
    }
    

    override func viewDidLoad() {
        super.viewDidLoad()

        // 异步加载用户头像
        iconView.sd_setImageWithURL(UserAccountViewModel.sharedUserAccount.avatarUrl, placeholderImage: UIImage(named: "avatar_default_big"))
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
}

// MARK: - 界面分类
extension WelcomeViewController {
    
    //添加对象设置约束
    private func setupUI() {
        // 1.添加子控件
        view.addSubview(iconView)
        view.addSubview(welcomeLabel)
        
        // 自动布局头像
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view.snp_centerX)
            make.bottom.equalTo(view.snp_bottom).offset(-200)
            make.height.equalTo(90)
            make.width.equalTo(90)
        }
        
        // label的布局
        welcomeLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_centerX)
            make.top.equalTo(iconView.snp_bottom).offset(16)
        }
        
    }
}

// MARK: - 动画分类
extension WelcomeViewController {
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        // 变更约束,已有的约束变更的时候，用update来更新，否则会报错
        iconView.snp_updateConstraints { (make) -> Void in
            make.bottom.equalTo(view.snp_bottom).offset(-view.bounds.height + 200)
        }
        
        // 动画
        welcomeLabel.alpha = 0
        
        UIView.animateWithDuration(1.2, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10, options: [], animations: { () -> Void in
            
            // 修改所有的动画属性
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                UIView.animateWithDuration(0.8, animations: { () -> Void in
                    self.welcomeLabel.alpha = 1
                    }, completion: { (_) -> Void in
                        
                       // 发送通知
                        NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchRootVieControllerNotification, object: nil)
                })
        }
    }

}

